package com.learn.dao.impl;

import com.learn.dao.AccountDao;
import com.learn.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/01/19
 * @description
 */
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public Account selectById(Integer id) {
        String sql = "select * from account where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), id);
    }

    @Override
    public int updateBalance(Account account) {
        String sql = "update account set balance = ? where id = ?";
        return jdbcTemplate.update(sql, account.getBalance(), account.getId());
    }

    @Override
    public int insert(Account account) {
        String sql = "insert into account value(?, ?)";
        return jdbcTemplate.update(sql, account.getId(), account.getBalance());
    }
}
